package com.obas.business.analogy.objectview
{
	import com.ease.component.managers.ErrorManager;
	import com.ease.component.managers.ProgressManager;
	import com.ease.framework.CommandCompletionEvent;
	import com.obas.business.analogy.vo.AnalogyParam;
	import com.obas.common.event.DimensionOperationEvent;
	import com.obas.common.util.ResourceUtil;
	import mx.events.ItemClickEvent;
	import flash.events.ContextMenuEvent;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.controls.Alert;
	
	[Event(name="{com.obas.common.event.DimensionOperationEvent.REQUEST_ID}",
         type="com.obas.common.event.DimensionOperationEvent")]
	
	public class UserBusinessExperienceObjectMediator extends EventDispatcher {
	
		private var _view:UserBusinessExperienceObjectView;
	
		
		[InjectableView]
		public function get view():UserBusinessExperienceObjectView {
			return _view;
		}
		
		public function set view(value:UserBusinessExperienceObjectView):void {
			_view = value;
//			_view.initCall = refresh;
			init();
		}
		
		private function init():void {
//			view.refreshBtn.addEventListener(MouseEvent.CLICK,onRefresh);
			
			_view.indicatorTypeToggleButtonBar.addEventListener(ItemClickEvent.ITEM_CLICK, refresh);
			
			
			refresh();
		}
		
		private function refresh(e:* = null):void {
			
			reloadHistoryRank();
		
		}
		
		private function reloadHistoryRank(): void {
			
			var dimensionOperationEvent:DimensionOperationEvent = new DimensionOperationEvent(DimensionOperationEvent.OPERATION_GET_USERAPP_HISTORY_RANK);

			dimensionOperationEvent.analogyParam=view.analogyParam;
			
			var indicatorTypes:IList =view.indicatorTypeToggleButtonBar.dataProvider as IList;			
			var indicatorType:Object =   indicatorTypes.getItemAt(view.indicatorTypeToggleButtonBar.selectedIndex);			
			dimensionOperationEvent.analogyParam.indicatorTypeCode=indicatorType.code;
			dimensionOperationEvent.analogyParam.indicatorTypeId=0;
			
			dispatchEvent(dimensionOperationEvent);
			
		}
		
		[ResponseHandler("{com.obas.common.event.DimensionOperationEvent.RETURN_ID}")]
		public function onDimensionOperationEvent(event:CommandCompletionEvent):void
		{
			//		  ProgressManager.hideProgress(_view);
			var request:DimensionOperationEvent=event.request as DimensionOperationEvent;
			if (event.error)
			{
				ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
				return;
			}
			
			switch (request.actionType)
			{
				case DimensionOperationEvent.OPERATION_GET_USERAPP_HISTORY_RANK:
					var arrayCollection:ArrayCollection=	event.result  as ArrayCollection;
					if(null!=arrayCollection) {
						view.lineChartData=arrayCollection;
//						view.historyRankLineChart.dataProvider=arrayCollection;
						
						var item:Object = arrayCollection.getItemAt(arrayCollection.length-1);
						if(null!=item) {
							var historyRankPieChartArrayCollection:ArrayCollection = new ArrayCollection();
							historyRankPieChartArrayCollection.addItem({name:"其好于", data:item.rank});
							historyRankPieChartArrayCollection.addItem({name:"其差于", data:1-item.rank});
							view.historyRankPieChart.dataProvider=historyRankPieChartArrayCollection;
							
							var sameBandwidthRankPieChartArrayCollection:ArrayCollection = new ArrayCollection();
							sameBandwidthRankPieChartArrayCollection.addItem({name:"其好于", data:item.sameBandwidthRank});
							sameBandwidthRankPieChartArrayCollection.addItem({name:"其差于", data:1-item.sameBandwidthRank});
							view.sameBandwidthRankPieChart.dataProvider=sameBandwidthRankPieChartArrayCollection;
							
//							var sameBrasRankPieChartArrayCollection:ArrayCollection = new ArrayCollection();
//							sameBrasRankPieChartArrayCollection.addItem({name:"其好于", data:item.sameBrasRank});
//							sameBrasRankPieChartArrayCollection.addItem({name:"其差于", data:1-item.sameBrasRank});
//							view.sameBrasRankPieChart.dataProvider=sameBrasRankPieChartArrayCollection;
//							
//							var sameSvlanRankPieChartArrayCollection:ArrayCollection = new ArrayCollection();
//							sameSvlanRankPieChartArrayCollection.addItem({name:"其好于", data:item.sameSvlanRank});
//							sameSvlanRankPieChartArrayCollection.addItem({name:"其差于", data:1-item.sameSvlanRank});
//							view.sameSvlanRankPieChart.dataProvider=sameSvlanRankPieChartArrayCollection;
//							
//							var sameOnuRankPieChartArrayCollection:ArrayCollection = new ArrayCollection();
//							sameOnuRankPieChartArrayCollection.addItem({name:"其好于", data:item.sameOnuRank});
//							sameOnuRankPieChartArrayCollection.addItem({name:"其差于", data:1-item.sameOnuRank});
//							view.sameOnuRankPieChart.dataProvider=sameOnuRankPieChartArrayCollection;
							
							
						}
					}
					break;
				
				//              case DataRefreshEvent.TYPE_ADD_UPDATE :
				//                 addOrUpdateItems(event.spec.data);
				//                 break;
				
			}
			
		}
	
	
	}
}